<?php 

class AskControl extends CommonControl{
	public function index(){
		$w=isset($_GET['w'])?(int)$_GET['w']:1;
		switch ($w) {
			case 1:
				$where=M('ask');
				break;
			case 2:
				$where=M('ask')->where('solve=0');
				break;
			case 3:
				$where=M('ask')->where('solve=1');
				break;
			case 4:
				$where=M('ask')->where('answer=0');
				break;
			default:
				$where=M('ask');
				break;
		}
		$data=$where->all();
		if(!is_null($data))$this->assign('ask',$data);
		$this->display();
	}
	/**
	 * [del_ask 删除问题]
	 * @return [type] [description]
	 */
	public function del_ask(){
			// $w=isset($_GET['w'])?(int)$_GET['w']:1;
			if(empty($_GET['asid']))$this->back();
			// 找到问题的悬赏金币和提问者uid
			$data=M('ask')->field('reward,uid,asid,solve')->where('asid='.$_GET['asid'])->find();
			// var_dump($data);die;
			// 删除问题
			$data1=M('ask')->where('asid='.$_GET['asid'])->delete();
			// 判断问题能否被找到
			if(!$data1||!$data)$this->error('问题不存在');
			// 如果问题没有被解决退还提问者金币
			if($data['solve']==0&&$data['reward']>0)$data2=M('user')->exec("update hd_user set point=point+{$data['reward']} where uid={$data['uid']}");
			// 删除答案
			$data3=M('answer')->where('asid='.$data['asid'])->delete();
			if(isset($data2)&&isset($data3))$this->error('删除成功且退还金币');
			// if(!$data2&&$data3)$this->success('删除成功',__ROOT__."?c=ask&w={$w}");
			 if(isset($data3))$this->error('删除成功');
			
	}

}
 ?>